package utils

import "time"

func FormatMonth(timeStamp int64) string {
	return time.Unix(timeStamp/1000, 0).Format("200601")
}

func FormatDay(timeStamp int64) int {
	return time.Unix(timeStamp/1000, 0).Day()
}

func FormatDate(timeStamp int64) string {
	return time.Unix(timeStamp/1000, 0).Format("20060102")
}

func GetYearMonthDay(year int, month int) int {
	//有31天的月份
	day31 := map[int]bool{
		1:  true,
		3:  true,
		5:  true,
		7:  true,
		8:  true,
		10: true,
		12: true,
	}
	if day31[month] == true {
		return 31
	}
	// 有30天的月份

	day30 := map[int]bool{
		4:  true,
		6:  true,
		9:  true,
		11: true,
	}
	if day30[month] == true {
		return 30
	}
	//计算平年还是闰年
	if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
		// 得出二月天数
		return 29
	}
	// 得出平年二月天数
	return 28
}

func LastMonth() (string, int) {
	year, month, _ := time.Now().Date()
	currentMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
	last := currentMonth.AddDate(0, 0, -1)
	lastMonth := last.Format("200601")
	endDay := last.Day()
	return lastMonth, endDay
}

func CurrentMonth() (string, int) {
	now := time.Now()
	month := now.Format("200601")
	day := now.Day()
	return month, day
}

func IsToday(ts int64) bool {
	d := time.Unix(ts/1e3, 0)
	now := time.Now()
	return d.Year() == now.Year() && d.Month() == now.Month() && d.Day() == now.Day()
}

func TodayStartTime() int64 {
	t := time.Now()
	addTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
	return addTime.UnixNano() / 1e6
}

func TodayEndTime() int64 {
	t := time.Now()
	addTime := time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
	return addTime.UnixNano()/1e6 - 1
}

func GetDayTime(timeStamp int64) int64 {
	t := time.Unix(timeStamp/1e3, 0)
	dayTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
	return dayTime.UnixMilli()
}

func GetPreNDayTime(timeStamp int64, n int) int64 {
	t := time.Unix(timeStamp/1e3, 0)
	ts := t.AddDate(0, 0, -n)
	dayTime := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location())
	return dayTime.UnixMilli()
}
